using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.EventSystems;


[Serializable]
/// <summary>
/// UI控制移动，根据一个Pointer事件组来获取移动的方向
/// </summary>
public class UIJoysTickComponent : BaseComponent
{

    public RectTransform background; // 摇杆背景
    public RectTransform m_handle;     // 摇杆手柄
    [ReadOnly]
    public Vector2 inputDirection;   // 输入方向
    [ReadOnly]
    public float InputPercent; //输入方向

    public TouchMethod TouchMethod; //方法事件

    public Action OnPointerDownAction; //按下事件
    public Action OnPointerUpAction; // 抬起事件

    private Vector2 input; //输入
    private Vector2 centerPosition; //摇杆中心位置

    private bool IsDown; //是否按下

    public bool IsCanMove = false; //是否可以输入（移动）
    public float LockFloat = 0.1f; //当拉到摇杆的比率超过这个值，才会算移动

    public GameObject tutorise;

    public override void OnAwake()
    {

    }

    public override void OnStart()
    {
        base.OnStart();

        centerPosition = m_handle.anchoredPosition;
        background.gameObject.SetActive(false);

        TouchMethod.PointerDownEvent += OnPointerDownEvent;
        TouchMethod.PointerUpEvent += OnPointerUpEvent;
        TouchMethod.PointerDragEvent += OnPointerDragEvent;

        IsCanMove = true;
    }

    private void OnPointerDownEvent(PointerEventData pointerEventData)
    {
        tutorise.gameObject.SetActive(false);
        if (!IsCanMove)
        {
            return;
        }
        

        OnPointerDownAction?.Invoke();
        IsDown = true;
        // background.gameObject.SetActive(true);
        background.anchoredPosition = Vector2.zero;
        Vector2 mousePosition = pointerEventData.position;
        Vector2 localMousePosition;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(background, mousePosition, null, out localMousePosition);
        background.anchoredPosition = localMousePosition;
    }

    private void OnPointerUpEvent(PointerEventData pointerEventData)
    {
        OnPointerUpAction?.Invoke();
        IsDown = false;
        background.gameObject.SetActive(false);
        background.anchoredPosition = new Vector2(0, -450);
        input = Vector2.zero;
        inputDirection = Vector2.zero;
        background.anchoredPosition = Vector2.zero;
        m_handle.anchoredPosition = Vector2.zero;

    }

    private void OnPointerDragEvent(PointerEventData pointerEventData)
    {
        if (!background.gameObject.activeSelf)
        {
            background.gameObject.SetActive(true);
        }
        Vector2 DragPosition = pointerEventData.position;
        Vector2 LocalDragPosition;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(background, DragPosition, null, out LocalDragPosition);

        float radius = background.rect.width / 2;
        input = LocalDragPosition;
        float distance = input.magnitude;
        
        if (distance > radius)
        {
            distance = radius;
            LocalDragPosition = LocalDragPosition.normalized * radius;
        }
        
        InputPercent = distance / radius;
        InputPercent = Mathf.Max(0.6f, InputPercent);
        m_handle.anchoredPosition = LocalDragPosition;

        // 计算方向并发送给火车
        if (distance > radius * LockFloat) // 添加死区
        {
            inputDirection = (m_handle.position - background.position).normalized;
            // if(train != null)
            // {
            //     train.SetDirection(inputDirection);
            // }
        }
        else
        {
            inputDirection = Vector2.zero;
            // if(train != null)
            // {
            //     train.SetDirection(Vector2.zero);
            // }
        }
    }
}
